﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Microsoft.Kinect;

using Coding4Fun.BoxingBots.Common;


namespace Coding4Fun.BoxingBots.UserConsole
{
    /// <summary>
    /// Interaction logic for ringAvatar.xaml
    /// </summary>
    public partial class ringAvatar : UserControl
    {
        private readonly List<WPF3DModel> _models = new List<WPF3DModel>();

        public ringAvatar()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(ringAvatarLoaded);
        }

        void ringAvatarLoaded(object sender, RoutedEventArgs e)
        {
            //wings = new PathFigure[] { figure_punchLeftInner, figure_punchLeftMiddle, figure_punchLeftOuter, figure_punchRightInner, figure_punchRightMiddle, figure_punchRightOuter };
            CreateViewportModels();
        }

        private void CreateViewportModels()
        {
            for (int i = 0; i < 20; i++)
            {
                WPF3DModel model = new WPF3DModel("mesh_cube");
                viewportModelsContainer2.Children.Add(model);
                model.InitialPosition(
                    5.0 * (Math.Cos((6.28 * i) / 20.0)),
                    5.0 * (Math.Sin((6.28 * i) / 20.0)),
                    -30.0 + (Math.Sin((6.28 * i) / 10.0)));
                _models.Add(model);
            }

            bodyRotationCenter.CenterX = 0.0;
            bodyRotationCenter.CenterY = 0.0;
            bodyRotationCenter.CenterZ = -30.0;

        }


        public void CrunchSkeleton()
        {
            //txtOut.Text = "CRUNCHING:";

            if (SkeletonProcessor.RawSkeleton == null)
            {
                //txtOut.Text += " NULL";

                foreach (WPF3DModel m in _models)
                {
                    m.EaseBack();
                }

                bodyRotationCenter.CenterX += (0 - bodyRotationCenter.CenterX) * .12;
                bodyRotationCenter.CenterY += (0 - bodyRotationCenter.CenterY) * .12;
                bodyRotationCenter.CenterZ += (-30 - bodyRotationCenter.CenterZ) * .12;
            }
            else
            {

                if (SkeletonProcessor.RawSkeleton.TrackingState == SkeletonTrackingState.Tracked)
                {
                    //txtOut.Text += "TRACKING";

                    int i = 0;
                    foreach (Joint joint in SkeletonProcessor.RawSkeleton.Joints)
                    {
                        if (joint.TrackingState == JointTrackingState.Tracked)
                        {
                            _models[i].Translate(
                                joint.Position.X * 8.0,
                                joint.Position.Y * 10.0,
                                joint.Position.Z * -10.0);
                            i++;
                        }
                    }
                    bodyRotationCenter.CenterX = 8.0 * SkeletonProcessor.RawSkeleton.Joints[JointType.HipCenter].Position.X;
                    bodyRotationCenter.CenterY = 10.0 * SkeletonProcessor.RawSkeleton.Joints[JointType.HipCenter].Position.Y;
                    bodyRotationCenter.CenterZ = -10.0 * SkeletonProcessor.RawSkeleton.Joints[JointType.HipCenter].Position.Z;
                }

            }

            setBodyBars(SkeletonProcessor.CoreAverageDelta.Z, SkeletonProcessor.CoreAverageDelta.X);
            shoulderLeftAngle.Angle = 24 - (SkeletonProcessor.PunchLeftDelta.Y * 48);
            shoulderRightAngle.Angle = 24 - (SkeletonProcessor.PunchRightDelta.Y * 48);




            //for (int i = 0; i < 6; i++)
            //{
            //    SetWing(wings[i], SkeletonProcessor.PunchValues[i], wingRadii[i % 3], wingMaximum[i % 3], wingWidth[i%3]);
            //}

            //txtOut.Text = "";
            //txtOut.Text += SkeletonProcessor.PivotShoulders.ToString("0.00") + " \n " + SkeletonProcessor.PivotHips.ToString("0.00") + " \n" + SkeletonProcessor.PivotAverage.ToString("0.00");

            //torsoTopAngle.Angle = 90.0 - SkeletonProcessor.PivotShoulders * 20.0;
            //torsoTopAngle.Angle = Math.Min(Math.Max(70.0, torsoTopAngle.Angle), 120.0);
            //torsoBottomAngle.Angle = 90.0 + SkeletonProcessor.PivotHips * 20.0;
            //torsoBottomAngle.Angle = Math.Min(Math.Max(70.0, torsoBottomAngle.Angle), 120.0);


            /*
            txtOut.Text += "FIXED:" + SkeletonProcessor.FixedCenter.X.ToString("0.0") + ", " + SkeletonProcessor.FixedCenter.Z.ToString("0.0") + "\n";
            txtOut.Text += "RING:" + SkeletonProcessor.RingCenter.X.ToString("0.0") + ", " + SkeletonProcessor.RingCenter.Z.ToString("0.0") + "\n";
            txtOut.Text += "DELTA:" + SkeletonProcessor.CoreAverageDelta.X.ToString("0.0") + ", " + SkeletonProcessor.CoreAverageDelta.Z.ToString("0.0") + "\n";
            txtOut.Text += "DIST:" + SkeletonProcessor.DistanceFromFixedCenter.ToString("0.0");
            */
        }

        public void UpdateUX()
        {
            myAngleRotation.Angle += .35;
        }

        public void setBodyBars(double forwardNormal, double sideNormal)
        {
            tdTranslate.X = 50.0 * sideNormal;
            tdTranslate.Y = 50.0 * forwardNormal;
            tdRotate.Angle = SkeletonProcessor.PivotAverage;

            // replace this with a moving TRIANGLE
            //barBodyFrontBack.Y = forwardNormal * 16.0;
            //barBodyLeftRight.X = sideNormal * 24.0;
        }

        //double[] wingRadii = { 164.0, 176.0, 188.0 };
        //const double BLADEWIDTH = 10.0;

        //double[] wingRadii = { 233.0, 259.0, 287.0 };
        //double[] wingWidth = { 14.0, 14.0, 18.0 };
        //const double BLADEWIDTH = 14.0;

        //private double[] wingMaximum = { .74, 1.12, 1.52 };
        //private PathFigure[] wings;

        //double[] velocities = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
        //double[] wingValues = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };

        /*
        private void SetWing(PathFigure blade, double angleNormal, double radius, double maxWingAngle, double bladeWidth)
        {
            double angle = angleNormal * maxWingAngle;
            Size innerRadius = new Size(radius, radius);
            Size outerRadius = new Size(radius + bladeWidth, radius + bladeWidth);
            Point innerArcStart = new Point(radius, 0.0);
            Point outerArcStart = new Point(radius + bladeWidth, 0.0);

            blade.Segments.Clear();
            blade.StartPoint = innerArcStart;
            blade.Segments.Add(new LineSegment(outerArcStart, true));

            bool isLarge = false;
            if (Math.Abs(angle) > 3.142) isLarge = true;

            blade.Segments.Add(new ArcSegment(
                new Point(Math.Cos(angle) * outerRadius.Height, Math.Sin(angle) * outerRadius.Height),
                outerRadius, 0, isLarge, SweepDirection.Counterclockwise, true));

            blade.Segments.Add(new LineSegment(
                new Point(Math.Cos(angle) * innerRadius.Width, Math.Sin(angle) * innerRadius.Width),
                true));

            blade.Segments.Add(new ArcSegment(innerArcStart, innerRadius, 0, isLarge, SweepDirection.Clockwise, true));

        }
         * */

    }
}
